package com.wonders.demo.module.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wonders.demo.core.shiro.ShiroUser;
import com.wonders.demo.module.mapper.UserMapper;
import com.wonders.demo.module.po.RolePo;
import com.wonders.demo.module.po.UserPo;
import com.wonders.demo.module.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wonders
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPo> implements UserService {
    @Override
    public UserPo getUserByName(String name) {
        return this.baseMapper.getUserByName(name);
    }

    @Override
    public ShiroUser shiroUser(UserPo userPo) {
        ShiroUser shiroUser = new ShiroUser();
        List<RolePo> roleList = this.baseMapper.getRolesByUserId(userPo.getId());
        List<Long> roleIds = roleList.stream().map(RolePo::getId).collect(Collectors.toList());

        List<String> menuNameList = null;
        if (!roleIds.isEmpty()) {
            menuNameList = this.baseMapper.getMenusByRoleIds(roleIds);
        }
        List<String> remarks = roleList.stream().map(RolePo::getRemarks).collect(Collectors.toList());
        shiroUser.setRoles(remarks);
        shiroUser.setMenus(menuNameList);
        shiroUser.setId(userPo.getId());
        shiroUser.setUsername(userPo.getUsername());
//        new Hashtable<>()
//                new HashMap<>()
//        new ArrayList<>()
        return shiroUser;
    }

    @Override
    public ShiroUser getAuthInfoByName(String userName) {
        return this.baseMapper.getAuthInfoByName(userName);
    }
}
